package com.ruoyi.information.service.back.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.information.domain.back.CooperateContentBack;
import com.ruoyi.information.mapper.back.CooperateBackMapper;
import com.ruoyi.information.domain.back.CooperateBack;
import com.ruoyi.information.service.back.ICooperateBackService;

/**
 * 商务合作Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-14
 */
@Service
public class CooperateBackServiceImpl implements ICooperateBackService
{
    @Autowired
    private CooperateBackMapper cooperateBackMapper;

    /**
     * 查询商务合作
     *
     * @param id 商务合作主键
     * @return 商务合作
     */
    @Override
    public CooperateBack selectCooperateBackById(Long id)
    {
        return cooperateBackMapper.selectCooperateBackById(id);
    }

    /**
     * 查询商务合作列表
     *
     * @param cooperateBack 商务合作
     * @return 商务合作
     */
    @Override
    public List<CooperateBack> selectCooperateBackList(CooperateBack cooperateBack)
    {
        return cooperateBackMapper.selectCooperateBackList(cooperateBack);
    }

    /**
     * 新增商务合作
     *
     * @param cooperateBack 商务合作
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCooperateBack(CooperateBack cooperateBack)
    {
        cooperateBack.setCreateTime(DateUtils.getNowDate());
        int rows = cooperateBackMapper.insertCooperateBack(cooperateBack);
        insertCooperateContent(cooperateBack);
        return rows;
    }

    /**
     * 修改商务合作
     *
     * @param cooperateBack 商务合作
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCooperateBack(CooperateBack cooperateBack)
    {
        cooperateBack.setUpdateTime(DateUtils.getNowDate());
        cooperateBackMapper.deleteCooperateContentByCooperateId(cooperateBack.getId());
        insertCooperateContent(cooperateBack);
        return cooperateBackMapper.updateCooperateBack(cooperateBack);
    }

    /**
     * 批量删除商务合作
     *
     * @param ids 需要删除的商务合作主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCooperateBackByIds(Long[] ids)
    {
        cooperateBackMapper.deleteCooperateContentByCooperateIds(ids);
        return cooperateBackMapper.deleteCooperateBackByIds(ids);
    }

    /**
     * 删除商务合作信息
     *
     * @param id 商务合作主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCooperateBackById(Long id)
    {
        cooperateBackMapper.deleteCooperateContentByCooperateId(id);
        return cooperateBackMapper.deleteCooperateBackById(id);
    }

    /**
     * 新增商务合作记录信息
     *
     * @param cooperateBack 商务合作对象
     */
    public void insertCooperateContent(CooperateBack cooperateBack)
    {
        List<CooperateContentBack> cooperateContentList = cooperateBack.getCooperateContentList();
        Long id = cooperateBack.getId();
        if (StringUtils.isNotNull(cooperateContentList))
        {
            List<CooperateContentBack> list = new ArrayList<CooperateContentBack>();
            for (CooperateContentBack cooperateContent : cooperateContentList)
            {
                cooperateContent.setCooperateId(id);
                list.add(cooperateContent);
            }
            if (list.size() > 0)
            {
                cooperateBackMapper.batchCooperateContent(list);
            }
        }
    }
}
